namespace Trader.Analyzing
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Interface;

    internal class ScenariosBuilder
    {
        private readonly int numOfRanges;
        private readonly List<IList<decimal>> periodsChanges;
        private decimal maxChange;
        private decimal minChange;

        public ScenariosBuilder(int numOfRanges)
        {
            this.numOfRanges = numOfRanges;
            periodsChanges = new List<IList<decimal>>();
        }

        public void Add(IList<decimal> periodChanges)
        {
            if (periodsChanges.Count > 0 && periodsChanges[0].Count != periodChanges.Count)
            {
                throw new ArgumentException("period is not in correct length");
            }

            minChange = Math.Min(minChange, periodChanges.Min());
            maxChange = Math.Max(maxChange, periodChanges.Max());

            periodsChanges.Add(periodChanges);
        }

        public IScenarios GetScenarios()
        {
            var scenarios = new Scenarios(minChange, maxChange, numOfRanges);

            foreach (var periodChanges in periodsChanges)
            {
                scenarios.AddEvent(periodChanges);
            }

            return scenarios;
        }
    }
}